package com.neuedu.mvc.controller;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 接受用户名、密码的参数  		admin、123456   、    root 123456
 *
 * - 成功： 在Session中设置一个登录状态  username ：  admin  、username ：  root
 *   - 重定向到index.jsp
 * - 失败： 重定向到login.jsp 提示登录失败
 */
@WebServlet(name = "LoginController", value = "/login")
public class LoginController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //具体的service代码暂时省略了，
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //判断是否存在改用户名 和密码

        if(    ("admin".equals(username)  && "123456".equals(password) )    ||
                ("root".equals(username)  && "123456".equals(password) )                   ){
            HttpSession session  = request.getSession();
            session.setAttribute("isLogin",true);
            session.setAttribute("username",username);

            response.sendRedirect(request.getContextPath()+"/index.jsp");

        }else{
            response.sendRedirect(request.getContextPath()+"/login.jsp?type=error");
        }





    }
}
